package ua.org.extjsbuilder.parser.scanner;


public class CharPattern {
	public static final CharPattern ALLOW_LETTERS_ONLY = 
		compile("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
	public static final CharPattern ALLOW_DIGITS_ONLY = 
		compile("0123456789");
	public static final CharPattern ALLOW_WHITE_SPACES = 
		compile(" \t\r\n");
	private static final int PATTERN_SIZE = 256;
	
	private boolean[] regex;
	
	private CharPattern(){
		regex = new boolean[PATTERN_SIZE];
	}
	
	public static CharPattern compile(CharSequence regex){
		CharPattern result = new CharPattern();
		for(int i=0;i<regex.length();i++){
			result.regex[(int)regex.charAt(i)]=true;
		}
		return result;
	}
	
	public CharPattern or(CharPattern other){
		CharPattern result = new CharPattern();
		for(int i=0;i<PATTERN_SIZE;i++){
			result.regex[i] = regex[i]||other.regex[i];
		}
		return result;
	}
	
	public CharPattern and(CharPattern other){
		CharPattern result = new CharPattern();
		for(int i=0;i<PATTERN_SIZE;i++){
			result.regex[i] = regex[i]&&other.regex[i];
		}
		return result;
	}
	
	public CharPattern not(){
		CharPattern result = new CharPattern();
		for(int i=0;i<PATTERN_SIZE;i++){
			result.regex[i] = !regex[i];
		}
		return result;
	}
	
	public boolean match(char c){
		if(c>255)return false;
		return regex[(int)c];
	}
}
